import { getUserInfo, putUserInfo } from "@/api/user"

export const useUserStore = defineStore('user',{
  state: () => {
    return {
      user: {} as any
    }
  },
  getters: {
    getUser: (state)=> {
      return state.user
    }
},
  actions: {
    getUserInfo(key?: string) {
      return key? this.user[key] : this.user
    },
    async updateUserInfo(data:any) {
      putUserInfo(data).then((result) => {
        getUserInfo()
      });
    },
    async refreshUserInfo() {
      getUserInfo().then((result) => {
        this.user = result.data
      });
    },
    clearUserInfo() {
      this.user = {}
    },
    setUserInfo(key:string, value: any) {
      this.user[key] = value
    }
  },
  persist: true
})